package com.cn.xiaonuo.slbfinances.slborders.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cn.xiaonuo.common.entity.BusinessMessage;
import com.cn.xiaonuo.dbs.core.annotation.DataSource;
import com.cn.xiaonuo.slbfinances.slborders.entity.SlScorePayment;
import com.cn.xiaonuo.slbfinances.slborders.mapper.SlScorePaymentMapper;
import com.cn.xiaonuo.slbfinances.slborders.param.SlScorePaymentParam;
import com.cn.xiaonuo.slbfinances.slborders.service.SlScorePaymentService;
import com.cn.xiaonuo.slbfinances.slborders.utils.Integer2BigdecimalUtils;
import com.cn.xiaonuo.slbfinances.slborders.vo.SlScorePaymentVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Admin
 * @description 针对表【sl_score_payment(业绩结款表)】的数据库操作Service实现
 * @createDate 2023-07-05 10:14:39
 */
@Service
public class SlScorePaymentServiceImpl extends ServiceImpl<SlScorePaymentMapper, SlScorePayment> implements SlScorePaymentService {
    @Autowired
    private SlScorePaymentMapper slScorePaymentMapper;

    @Override
    @DataSource(name = "jyb_soole_data_hn")
    public BusinessMessage list(SlScorePaymentParam param) {
        BusinessMessage message = new BusinessMessage();
        PageHelper.startPage(param.getPageNo(), param.getPageSize());
        List<SlScorePaymentVo> list = slScorePaymentMapper.list(param);
        message.setData(new PageInfo<>(list));
        message.setSuccess(true);
        return message;
    }

    @Override
    @DataSource(name = "jyb_soole_data_hn")
    public BusinessMessage total(SlScorePaymentParam param) {
        BusinessMessage message = new BusinessMessage();
        List<SlScorePayment> list = slScorePaymentMapper.total(param);
        //分类求和
        int unSettleSum = list.stream().filter(slScorePayment -> slScorePayment.getStatus() == 0).mapToInt(SlScorePayment::getStoreSettlementAmount).sum();//未结算金额
        int settleSum = list.stream().filter(slScorePayment -> slScorePayment.getStatus() == 1).mapToInt(SlScorePayment::getStoreSettlementAmount).sum();//已结算金额
        int scoreAmountSum = list.stream().filter(slScorePayment -> slScorePayment.getScoreAmount() != null).mapToInt(SlScorePayment::getScoreAmount).sum();//邀请人业绩结算金额
        int gwjSum = list.stream().filter(slScorePayment -> slScorePayment.getTotalGwj() != null).mapToInt(SlScorePayment::getTotalGwj).sum();//积分订单金额
        //四舍五入,保留2位小数
        Map<String,Object> map = new HashMap<>();
        map.put("unSettlePrice",Integer2BigdecimalUtils.round(unSettleSum));
        map.put("settlePrice", Integer2BigdecimalUtils.round(settleSum));
        map.put("scoreAmountPrice",Integer2BigdecimalUtils.round(scoreAmountSum));
        map.put("gwjPrice",Integer2BigdecimalUtils.round(gwjSum));
        message.setData(map);
        message.setSuccess(true);
        return message;
    }
}




